/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Admin;

import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import supermarket.*;
import model.*;

/**
 *
 * @author koag
 */
public class ProductsPanel extends javax.swing.JPanel {

    DBmanager db;
    private int listSelectedIndex;
    List<Product> Products;
    private DefaultListModel model;

    /**
     * Creates new form ProductsPanel
     */
    public ProductsPanel(DBmanager db) {
        this.db = db;
        initComponents();
        updateProductList();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ProductsList = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        b_delete = new javax.swing.JButton();
        b_create = new javax.swing.JButton();
        b_save = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        kodikos = new javax.swing.JTextField();
        pontoi = new javax.swing.JTextField();
        timi = new javax.swing.JTextField();
        onoma = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        jPanel1.setBackground(new java.awt.Color(248, 127, 7));
        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 24)); // NOI18N
        jLabel1.setText("Διαχείριση Προϊόντων");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        ProductsList.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        ProductsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ProductsListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(ProductsList);

        b_delete.setText("Διαγραφή");
        b_delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_deleteActionPerformed(evt);
            }
        });

        b_create.setText("Δημιουργία");
        b_create.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_createActionPerformed(evt);
            }
        });

        b_save.setText("Αποθήκευση");
        b_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_saveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(b_create, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(b_delete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(b_save))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(b_create)
                .addGap(18, 18, 18)
                .addComponent(b_save)
                .addGap(18, 18, 18)
                .addComponent(b_delete)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel2.setText("Κωδικός :");

        jLabel3.setText("Ονομα    :");

        jLabel4.setText("Τιμή        :");

        jLabel5.setText("Πόντοι   :");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(onoma, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(pontoi, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)
                            .addComponent(kodikos, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(timi, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(kodikos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel3)
                    .addComponent(onoma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel4)
                    .addComponent(timi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel5)
                    .addComponent(pontoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addGap(6, 6, 6))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(15, 15, 15))
        );
    }// </editor-fold>//GEN-END:initComponents
// Ενημερώνει την λίστα

    private void updateProductList() {
        // Αρχικοποίησε Μεταβλητές
        this.listSelectedIndex = 0;
        this.onoma.setText("");
        this.kodikos.setText("");
        this.pontoi.setText("");
        this.timi.setText("");

        // Ζήτα την λίστα με τα προιόντα απο την ΒΔ.
        Products = db.getProductList();

        // Δημιούργησε DefaultListModel και συσχετισέτο με την λίστα μας
        model = new DefaultListModel();
        ProductsList.setModel(model);

        // Καθάρισε Την λίστα μας
        model.removeAllElements();

        //Βάζουμε τα στοιχεία στο model άρα και στην λίστα.
        for (int i = 0; i < Products.size(); i++) {
            String productLine = toString(Products.get(i));
            model.addElement(i + 1 + ". " + productLine);
        }
        // καλού κακού κάντο και αυτό
        ProductsList.revalidate();
    }

    // φτιάνει το string για κάθε Προιόν στην λίστα 
    private String toString(Product p) {
        String r = p.getName() + ",  " + p.getCode() + ",  " + p.getPrice() + ",  " + p.getPoints();
        return r;
    }

    private void ProductsListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ProductsListMouseClicked
        // βρές την επιλεγμένη γραμμή
        this.listSelectedIndex = ProductsList.getSelectedIndex();
        // Ενημέρωσε τα textBoxes
        Product p = new Product();
        p = Products.get(this.listSelectedIndex);
        this.onoma.setText(p.getName());
        this.kodikos.setText(p.getCode());
        this.timi.setText(Float.toString(p.getPrice()));
        this.pontoi.setText(Integer.toString(p.getPoints()));
    }//GEN-LAST:event_ProductsListMouseClicked

    private void b_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_saveActionPerformed
        if (this.listSelectedIndex == 0) {
            return;
        }

        try {
            //Διάβασε το προιόν απο την Βάση
            Product p = db.getProductById(this.Products.get(this.listSelectedIndex).getProductId());
            // Ελεγξε τα πεδία τιμή και πόντοι ότι έχουν σωστές τιμές
            // Ενημέρωσε τα πεδία του με τις αλλαγές του χρήστη
            p.setCode(this.kodikos.getText());
            p.setName(this.onoma.getText());
            p.setPoints(Integer.parseInt(this.pontoi.getText()));
            p.setPrice(Float.parseFloat(this.timi.getText()));
            // Και στείλτο για ενημέρωση στην ΒΔ.
            int result = db.updateProduct(p);

            // Ελεγξε αν ήταν επιτυχής η ενημέρωση
            // και ενημέρωσε χρήστη
            if (result == 1) {
                JOptionPane.showMessageDialog(this, SuperMarket.msgSuccModification, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, SuperMarket.msgUNSuccModification, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
            }

            // Επαναεισήγαγε την λίστα των Προιόντων
            updateProductList();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, SuperMarket.msgErrorinData, SuperMarket.appTitle, JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_b_saveActionPerformed

    private void b_createActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_createActionPerformed

        if (this.onoma.getText().equals("")
                || this.pontoi.getText().equals("")
                || this.timi.getText().equals("")
                || this.kodikos.getText().equals("")) {
            return;
        }

        try {
            //Φτιάξε ένα Προιόν
            Product p = new Product();
            // Ελεγξε τα πεδία τιμή και πόντοι ότι έχουν σωστές τιμές
            // Ενημέρωσε τα πεδία του με τις αλλαγές του χρήστη
            p.setCode(this.kodikos.getText());
            p.setName(this.onoma.getText());
            p.setPoints(Integer.parseInt(this.pontoi.getText()));
            p.setPrice(Float.parseFloat(this.timi.getText()));
            // Και στείλτο για ενημέρωση στην ΒΔ.
            int result = db.insertProduct(p);

            // Ελεγξε αν ήταν επιτυχής η ενημέρωση
            // και ενημέρωσε χρήστη
            if (result == 1) {
                JOptionPane.showMessageDialog(this, SuperMarket.msgSuccModification, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(this, SuperMarket.msgUNSuccModification, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
            }

            // Επαναεισήγαγε την λίστα των Προιόντων
            updateProductList();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, SuperMarket.msgErrorinData, SuperMarket.appTitle, JOptionPane.ERROR_MESSAGE);
        }

    }//GEN-LAST:event_b_createActionPerformed

    private void b_deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_deleteActionPerformed
        if (this.listSelectedIndex == 0) {
            return;
        }
        //Βρες ποιο Product πρέπει να Διαγραφεί
        Product p = new Product();
        p = Products.get(this.listSelectedIndex);
        // Και στείλτο για Διαγραφή στην ΒΔ.
        int result = db.deleteProduct(p);

        // Ελεγξε αν ήταν επιτυχής η Διαγραφή
        // και ενημέρωσε χρήστη
        if (result == 1) {
            JOptionPane.showMessageDialog(this, SuperMarket.msgSuccDeletion, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, SuperMarket.msgUNSuccDeletion, SuperMarket.appTitle, JOptionPane.PLAIN_MESSAGE);
        }

        // Επαναεισήγαγε την λίστα των Καταστημάτων
        updateProductList();
    }//GEN-LAST:event_b_deleteActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList ProductsList;
    private javax.swing.JButton b_create;
    private javax.swing.JButton b_delete;
    private javax.swing.JButton b_save;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField kodikos;
    private javax.swing.JTextField onoma;
    private javax.swing.JTextField pontoi;
    private javax.swing.JTextField timi;
    // End of variables declaration//GEN-END:variables
}
